package fr.fermeManager.application.views.utils;

import java.beans.PropertyChangeSupport;
import java.util.prefs.Preferences;

import org.jdesktop.swingx.auth.UserNameStore;

/**
 * Saves the user names in Preferences. Because any string could be part of the
 * user name, for every user name that must be saved a new Preferences key/value
 * pair must be stored.
 * 
 */

public class StoreUserName extends UserNameStore {

	/**
	 * Saves the user names in Preferences. Because any string could be part of
	 * the user name, for every user name that must be saved a new Preferences
	 * key/value pair must be stored.
	 * 
	 */

	// The key for one of the preferences
	private static final String USER_KEY = "usernames";
	private static final String NUM_KEY = "usernames.length";
	
	private static final String LAST_USER = "last.user";

	/**
	 * A name that is used when retrieving preferences. By default, the app name
	 * is "default&quot. This should be set by the application if the
	 * application wants it's own list of user names.
	 */
//	private static final String DEFAULT_APP_NAME = "default";

	/**
	 * The preferences node
	 */
	private Preferences prefs;

	/**
	 * Contains the user names. Since the list of user names is not frequently
	 * updated, there is no penalty in storing the values in an array.
	 */
	private String[] userNames;
	
	private String lastUser;

	/**
	 * Used for propogating bean changes
	 */
	private PropertyChangeSupport pcs;

	/**
	 * Creates a new instance of DefaultUserNameStore
	 */

	public StoreUserName() {
		System.err.println("Creating");
		pcs = new PropertyChangeSupport(this);
		userNames = new String[0];
		loadUserNames();
	}

	/**
	 * Loads the user names from Preferences
	 */
	public void loadUserNames() {
		System.err.println("Loading usernames");
		initPrefs();
		if (prefs != null) {
			int n = prefs.getInt(NUM_KEY, 0);
			lastUser = prefs.get(LAST_USER,"");
			String[] names = new String[n];
			for (int i = 0; i < n; i++) {
				names[i] = prefs.get(USER_KEY + "." + i, null);
			}
			setUserNames(names);
		}
	}

	/**
	 * Saves the user names to Preferences
	 */
	public void saveUserNames() {
		System.err.println("Saving Usernames");
		initPrefs();
		if (prefs != null) {
			prefs.putInt(NUM_KEY, userNames.length);
			prefs.put(LAST_USER, lastUser);
			for (int i = 0; i < userNames.length; i++) {
				prefs.put(USER_KEY + "." + i, userNames[i]);
			}
		}
	}

	/**
	 * @inheritDoc
	 */
	public String[] getUserNames() {
		System.err.println("Get Usernames");
		return userNames;
	}

	/**
	 * @inheritDoc
	 */
	public void setUserNames(String[] userNames) {
		System.err.println("Setting userNames array");
		if (this.userNames != userNames) {
			String[] old = this.userNames;
			this.userNames = userNames == null ? new String[0] : userNames;
			pcs.firePropertyChange("userNames", old, this.userNames);
		}
	}

	/**
	 * Add a username to the store.
	 * 
	 * @param name
	 */
	public void addUserName(String name) {
		System.err.println("Adding " + name);
		lastUser = name;
		if (!containsUserName(name)) {
			String[] newNames = new String[userNames.length + 1];
			for (int i = 0; i < newNames.length; i++) {
				newNames[i] = userNames[i];
			}
			newNames[newNames.length - 1] = name;
			setUserNames(newNames);

		}
	}

	/**
	 * Removes a username from the list.
	 * 
	 * @param name
	 */
	public void removeUserName(String name) {
		System.err.println("Removing " + name);
		if (containsUserName(name)) {
			String[] newNames = new String[userNames.length - 1];
			int index = 0;
			for (String s : userNames) {
				if (!s.equals(name)) {
					newNames[index++] = s;
				}
			}
			setUserNames(newNames);
		}
	}

	/**
	 * @inheritDoc
	 */
	public boolean containsUserName(String name) {
		System.err.println("Contains " + name);
		for (String s : userNames) {
			if (s.equals(name)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @return Returns Preferences node in which the user names will be stored
	 */
	public Preferences getPreferences() {
		System.err.println("get prefs");
		return prefs;
	}

	/**
	 * @param prefs
	 *            the Preferences node to store the user names in. If null, or
	 *            undefined, then they are stored in
	 *            /org/jdesktop/swingx/auth/DefaultUserNameStore.
	 */
	public void setPreferences(Preferences prefs) {
		System.err.println("set prefs");
		initPrefs();
		if (this.prefs != prefs) {
			Preferences old = this.prefs;
			this.prefs = prefs;
			pcs.firePropertyChange("preferences", old, prefs);
			// if prefs is null, this next method will create the default prefs
			// node
			loadUserNames();
		}
	}

	/**
	 * Creates the default prefs node
	 */
	private void initPrefs() {
		System.err.println("init prefs");
		if (prefs == null) {
			prefs = Preferences.userNodeForPackage(fr.fermeManager.application.utils.Lanceur.class);
			// Use a relative path
			// prefs = Preferences.userRoot().node("/IS");
		}
	}

	/**
	 * @return the lastUser
	 */
	public String getLastUser() {
		return lastUser;
	}

	// http://ignite.digitalignition.net/articlesexamples/swingx-jxloginpanel/
}